package com.tbit.uqbike.webmanager.controller;

import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AddCountFee;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AddCountFeeService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.service.business.impl.SensitivenessServiceImpl;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author chenmengran
 * @Description: 运营区域计费配置
 * @date 2019\12\26 002616:45
 */
@Validated
@RestController
@RequestMapping(value = "/fee", produces = BaseController.MEDIA_JSON_UTF8)
public class AddCountFeeController extends BaseController {
	@Autowired
	private AddCountFeeService addCountFeeService;
	@Autowired
	private SensitivenessServiceImpl sensitivenessService;
	@Autowired
	private OperaPswService operaPswService;

	@RequestMapping("/setFee")
	@OperateLog(module = ModuleEnum.ACCOUNTFEE, methods = MethodEnum.COUNTFEE_ADD)
	public JsonResult setFee(AddCountFee addCountFee, String operaPsw, HttpServletRequest request) {
		LoginInfo loginInfo = getLogin(request);
		if (!loginInfo.type.equals(UserConstant.USER_AGENT)) {
			Assert.notNull(addCountFee.getAccountId(), "ver.accountId.notNull");
		} else {
			addCountFee.setAccountId(loginInfo.id);
		}
		// 计费规则 合法性校验
		validFee(addCountFee);
		addCountFee.checkCapFee();

		/**操作密码校验*/
		operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), operaPsw);

		addCountFeeService.add(loginInfo, addCountFee);
		sensitivenessService.sendMessage(addCountFee.getAccountId(), 1);
		return JsonResult.ok();
	}

	@RequestMapping("/getFee")
	public JsonResult getFee(Integer accountId, HttpServletRequest request) {
		LoginInfo loginInfo = getLogin(request);
		if (!loginInfo.type.equals(UserConstant.USER_AGENT)) {
			Assert.notNull(accountId, "ver.accountId.notNull");
		} else {
			accountId = loginInfo.id;
		}
		AddCountFee addCountFee = addCountFeeService.getFee(loginInfo, accountId);

		return JsonResult.ok(addCountFee);
	}

	@RequestMapping("/getMachineFee")
	public JsonResult getMachineFee(String userCode, HttpServletRequest request) {
		Assert.notNull(userCode, "ver.userCode.notNull");
		AddCountFee addCountFee = addCountFeeService.getByUserCode(userCode);
		return JsonResult.ok(addCountFee);
	}

	/**
	 * @Description: 添加车辆计费规则
	 * @Param: [addCountFee]
	 * @return: com.tbit.uqbike.object.view.JsonResult
	 * @Author: Jing
	 * @Date: 2021/6/1
	 */
	@PostMapping
	@OperateLog(module = ModuleEnum.ACCOUNTFEE, methods = MethodEnum.COUNTFEE_ADD)
	public JsonResult add(@RequestBody AddCountFee addCountFee) {
		RequestContext context = getLogin(ServletUtils.getRequest()).context;
		addCountFee.checkCapFee();

		/**操作密码校验*/
		operaPswService.checkOperaPsw(getLogin(ServletUtils.getRequest()), OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(),
				addCountFee.getOperaPsw());

		return JsonResult.toAjax(addCountFeeService.add(addCountFee), context);
	}

	/***
	 * @Description: 查询区域计费规则
	 * @Param: [addCountFee]
	 * @return: com.tbit.uqbike.object.view.JsonResult
	 * @Author: Jing
	 * @Date: 2021/6/1
	 */
	@GetMapping
	public JsonResult list(AddCountFee addCountFee) {
		startPage();
		List<AddCountFee> list = addCountFeeService.getMachineFeeList(addCountFee);
		return JsonResult.ok(list, (int) new PageInfo<>(list).getTotal());
	}

	@GetMapping("/{feeId}")
	public JsonResult getInfo(@PathVariable Integer feeId) {
		return JsonResult.ok(addCountFeeService.getInfo(feeId));
	}

	@DeleteMapping("/{feeIds}")
	@OperateLog(module = ModuleEnum.ACCOUNTFEE, methods = MethodEnum.COUNTFEE_DEL)
	public JsonResult remove(@PathVariable Integer[] feeIds) {
		RequestContext context = getLogin(ServletUtils.getRequest()).context;
		return JsonResult.toAjax(addCountFeeService.deleteRoleByIds(feeIds), context);
	}

	private void validFee(AddCountFee addCountFee) {
		// 封顶时间计费规则条件必须比起步计费规则更广
		if (Objects.nonNull(addCountFee.getCapTime()) && addCountFee.getCapTime() > 0 && addCountFee.getCapTime() < addCountFee.getBaseTime()){
			throw new BaseException("封顶计费的时间必须大于起步计费的时间");
		}

		// 封顶里程计费规则条件必须比起步计费规则更广
		if (Objects.nonNull(addCountFee.getCapMile()) && addCountFee.getCapMile() > 0 && addCountFee.getCapMile() < addCountFee.getBaseMile()){
			throw new BaseException("封顶计费的里程必须大于起步计费的时间");
		}

		if (Objects.nonNull(addCountFee.getNoParkAreaMoney()) && (addCountFee.getNoParkAreaMoney() < 0 || addCountFee.getNoParkAreaMoney() > 99900)){
			throw new BaseException("请输入正确的金额");
		}
	}

}
